package com.goudan.demo.common.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.goudan.demo.common.errorCode.CommonErrorCode;

import java.io.Serializable;

public class CommonResult<T> implements Serializable {
    private String code;
    private String message;
    private T data;

    public CommonResult() {
    }

    public static <T> CommonResult<T> error(CommonResult<?> result) {
        return error(result.getCode(), result.getMessage());
    }

    public static <T> CommonResult<T> error(String code, String message) {
        CommonResult<T> result = new CommonResult();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> CommonResult<T> error(String code, String message, T data) {
        CommonResult<T> result = new CommonResult();
        result.setData(data);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result = new CommonResult();
        result.setData(data);
        result.setCode(CommonErrorCode.SUCCESS.code());
        result.setMessage("");
        return result;
    }

    public static <T> CommonResult<T> success(String code, String message, T data) {
        CommonResult<T> result = new CommonResult();
        result.setData(data);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return CommonErrorCode.SUCCESS.code().equals(this.code);
    }

    @JsonIgnore
    public boolean isError() {
        return !this.isSuccess();
    }

    public String toString() {
        return "CommonResult{code=" + this.code + ", message='" + this.message + '\'' + ", data=" + this.data + '}';
    }

}
